package com.smithlibraties.util.annotation.register.loader;

import com.smithlibraties.SmithLibraries;
import com.smithlibraties.util.annotation.AnnotationClassGetter;
import com.smithlibraties.util.annotation.register.RegisterEntity;
import cpw.mods.fml.common.registry.EntityRegistry;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;

import java.util.ArrayList;
import java.util.List;

public class EntityRegister {
    private static int id;
    List<EntityAttribute> list = new ArrayList<>();

    public EntityRegister() {
        List<Class> entityCollections = AnnotationClassGetter.getAllClassByInterface(SmithLibraries.getAllClasses(), RegisterEntity.class);//获取实现RegisterClass注释的类的List
        for (Class clazz : entityCollections) {//开始集中继承IRegister接口的的类的属性的List
            list = (EntityRegister.getAnnotationList(clazz));
        }
        for (EntityAttribute registerAttribute : list) {
            if (registerAttribute.getSolidColor() == 0 && registerAttribute.getSpotColor() == 0)
                registerEntity(registerAttribute.getM(), registerAttribute.getName());
            else
                registerEntity(registerAttribute.getM(), registerAttribute.getName(), registerAttribute.getSolidColor(), registerAttribute.getSpotColor());
        }
    }

    private static void registerEntity(Class<? extends Entity> entityClass, String name) {
        EntityRegistry.registerGlobalEntityID(entityClass, name, id);
        EntityRegistry.registerModEntity(entityClass, name, id, SmithLibraries.instance, 64, 2, true);
        id++;
    }

    private static void registerEntity(Class<? extends Entity> entityClass, String name, int solidColor, int spotColor) {
        EntityList.addMapping(entityClass, name, id, solidColor, spotColor);
        id++;
    }


    private static List<EntityAttribute> getAnnotationList(Class clazz) {
        List<EntityAttribute> list1 = new ArrayList<>();
        if (clazz.isAnnotationPresent(RegisterEntity.class)) {
            RegisterEntity registerEntity = (RegisterEntity) clazz.getAnnotation(RegisterEntity.class);
            if (clazz.isAnnotationPresent(RegisterEntity.EntityEgg.class)) {
                RegisterEntity.EntityEgg registerEntityEgg = (RegisterEntity.EntityEgg) clazz.getAnnotation(RegisterEntity.EntityEgg.class);
                list1.add(new EntityAttribute(clazz, registerEntity.entityName(), registerEntityEgg.solidColor(), registerEntityEgg.spotColor()));
            } else list1.add(new EntityAttribute(clazz, registerEntity.entityName()));
        }
        return list1;
    }
}
